import {
	addPending, removePending, cancelPending,
} from './cancelRequest';
import axios from "axios";
import {  Toast } from 'antd-mobile'
/* @desc:不拦截的请求名 */
const unInterceptorUrl = []

//不弹loading的请求名
const unLoadingUrl = []



/* @desc:检测请求名是否需要拦截 */
function checkUrl(resp) {
	let arr = resp.config.url.split('?')[0].split('/'),
		flag = true
	unInterceptorUrl.forEach(item => {
		if (item === arr[arr.length - 2] + '/' + arr[arr.length - 1]) {
			flag = false
		}
	})
	return flag
}

/* @desc:检测不弹loading的请求名*/
function checkLoadUrl(resp) {
	let arr = resp.url.split('/'),
		flag = true
	unLoadingUrl.forEach(item => {
		if (item === arr[arr.length - 2] + '/' + arr[arr.length - 1]) {
			flag = false
		}
	})
	return flag
}


export const request = function (config) {
	// 发送请求前首先检查该请求是否已经重复，重复则进行取消并移除
	cancelPending(config)
	// 添加该请求到pendingList中
	addPending(config)
	if (checkLoadUrl(config)) {
		// Loading.service({
		//   fullscreen: true
		// })
	}
	return config
}

export const response = function (response) {
	// 请求成功去除pengding状态
	removePending(response.config);
	// Loading.service().close();
	if (response.data.status === 404) { 
		Toast.show({
			content: '404' 
		})
	}
	if (response.data.status !== 200 && checkUrl(response)) { 
		Toast.show({
			content: 'response.data.message' 
		})
	}
	return response
}